package com.example.oj.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.oj.entity.Competition;
import com.example.oj.entity.dto.competition.CompetitionQueryRequest;
import com.example.oj.entity.dto.competition.CompetitionSaveRequest;
import com.example.oj.entity.dto.competition.ResultQueryRequest;
import com.example.oj.entity.vo.CompetitionResultVO;
import com.example.oj.entity.vo.CompetitionVO;
import com.example.oj.entity.vo.QuestionVO;
import com.example.oj.service.CompetitionResultService;
import com.example.oj.service.CompetitionService;
import com.example.oj.service.UserRegistrationService;
import com.example.ojcommon.common.BaseResponse;
import com.example.ojcommon.common.ErrorCode;
import com.example.ojcommon.common.ResultUtils;
import com.example.ojcommon.exception.ThrowUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/competition")
public class CompetitionController {

    @Resource
    private CompetitionService competitionService;

    @Resource
    private UserRegistrationService registrationService;

    @Resource
    private CompetitionResultService resultService;

    @PostMapping("/list/page")
    public BaseResponse<Page<CompetitionVO>> listPageCompetitionVO(@RequestBody CompetitionQueryRequest queryRequest, HttpServletRequest request){
        long current = queryRequest.getCurrent();
        long pageSize = queryRequest.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(pageSize > 20, ErrorCode.PARAMS_ERROR);
        List<Competition> list = competitionService.getCompetitionPage(queryRequest);
        Page<Competition> questionPage = new Page<>(current, pageSize);
        questionPage.setRecords(list);
        return ResultUtils.success(competitionService.getCompetitionVOPage(questionPage));
    }

    @GetMapping("/getById")
    public BaseResponse<CompetitionVO> getCompetitionById(Long id){
        Competition competition = competitionService.getById(id);
        CompetitionVO competitionVO = competitionService.getCompetitionVO(competition);
        return ResultUtils.success(competitionVO);
    }

    @GetMapping("/get/question")
    public BaseResponse<List<QuestionVO>> getQuestionList(Long id){
        List<QuestionVO> list = competitionService.getQuestionListByCid(id);
        return ResultUtils.success(list);
    }

    @GetMapping("/get/isRegistry")
    public BaseResponse<Long> getIsRegistry(Long userId, Long competitionId){
        Long isRegistry = registrationService.getIsRegistry(userId,competitionId);
        return ResultUtils.success(isRegistry);
    }

    @GetMapping("/update/userRegistry")
    public BaseResponse<Integer> updateUserRegistry(Long userId, Long competitionId,Integer isAdd){
        Integer isRegistry = registrationService.updateUserRegistry(userId,competitionId,isAdd);
        return ResultUtils.success(isRegistry);
    }

    @PostMapping("/save/competition")
    public BaseResponse<Integer> saveCompetition(@RequestBody CompetitionSaveRequest data, HttpServletRequest request){
        Integer res = competitionService.saveCompetition(data,request);
        return ResultUtils.success(res);
    }

    @PostMapping("/delete/competition")
    public BaseResponse<Integer> deleteCompetition(Long id, HttpServletRequest request){
        Integer res = competitionService.deleteCompetition(id);
        return ResultUtils.success(res);
    }

    @PostMapping("/update/competitionState")
    public BaseResponse<Integer> updateCompetitionState(Long id,Integer state){
        Integer res = competitionService.updateCompetitionState(id,state);
        return ResultUtils.success(res);
    }

    @PostMapping("/get/result")
    public BaseResponse<Page<CompetitionResultVO>> getCompetitionResult(@RequestBody ResultQueryRequest request){
        Page<CompetitionResultVO> resultVOList = resultService.selectResultById(request);
        return ResultUtils.success(resultVOList);
    }

}
